import { IExportParamModelData, Number3 } from "@manycore/custom-miniapp-sdk";

/** 发送一次IDP消息并监听相应的返回消息 */
export async function listenIdpMsgOnce(postActionName: string, acceptActionName = postActionName) {
  return new Promise<any>((res, rej) => {
    const handler = (event: MessageEvent) => {
      if (event.data.action === acceptActionName) {
        res(event.data.value)
      }
      window.removeEventListener('message', handler);
    }
    window.addEventListener('message', handler)
    window.parent.postMessage({ action: postActionName }, '*')
  })
}
/** 获取最底层的模型 */
export function getFloorModels(paramModel: IExportParamModelData) {
  const floorModels: IExportParamModelData[] = []
  function recursion(models: IExportParamModelData[]) {
    models.forEach((model) => {
      if (!model.subModels) {
        floorModels.push(model)
        return
      }
      recursion(model.subModels)
    })
  }
  recursion(paramModel.subModels)
  return floorModels
}
/** 获取[x, y, z]格式的数据 */
export function xyz(o: Number3) {
  const { x, y, z } = o
  return [x, y, z] as [typeof x, typeof y, typeof z]
}